package com.zhb.service;

import com.zhb.Dto.Result;
import com.zhb.untils.OperateLinux;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

/**
 * @Description TODO
 * @Author Bigbo
 * @Date 2023/5/3 18:07
 * @Version 1.0
 */
@Service
public class SMservice {

    @Value("${linux.hostname}")
    private  String hostname;
    @Value("${linux.password}")
    private  String password;
    @Value("${linux.port}")
    private  int port;
    @Value("${linux.username}")
    private  String username;

    static {
        System.out.println("连接linux 服务器：");
        try {
            OperateLinux.connect("47.94.4.57", "root", "****", 22);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("服务器已连接");
    }
    // public  SMservice(String hostname, String password, int port, String username) {
    //     this.hostname = hostname;
    //     this.password = password;
    //     this.port = port;
    //     this.username = username;
    // }
    // @Bean
    // public SMservice SMservice() {
    //     SMservice sMservice = new SMservice(hostname,password,port,username);
    //     return sMservice;
    // }

    // SM4-CBC加密
    public Result sm4CbcEncrypt(String plaintext, String key) throws Exception {
        String command = "echo " + plaintext + " | /root/tongsuo/bin/tongsuo enc -K " + key + " -e -sm4-cbc " +
                "-iv 1fb2d42fb36e2e88a220b04f2e49aa13 -nosalt -base64";
        System.out.println(command);
        // return OperateLinux.execute(command,"UTF-8");
        return OperateLinux.executeShell(command);

    }


    // SM4-CBC解密
    public Result sm4CbcDecrypt(String ciphertext, String key) throws Exception {
        String command = "echo " + ciphertext + "| /root/tongsuo/bin/tongsuo enc -K " + key + " -d -sm4-cbc " +
                "-iv 1fb2d42fb36e2e88a220b04f2e49aa13 -nosalt -base64";
        System.out.println(command);
        return OperateLinux.executeShell(command);
    }


// sm3杂凑算法

    public Result sm3Dgst(String ciphertext) throws Exception {
        String command = "echo -n " + ciphertext + " | /root/tongsuo/bin/tongsuo dgst -sm3";
        return OperateLinux.executeShell(command);
    }

    // sm2签名验证
    public Result sm2Verify(String sig_file, String file_path) throws Exception {
        String command = "/root/tongsuo/bin/tongsuo dgst -sm3 -verify ./sm2pub.key -signature {} {}".format(sig_file, file_path);
        return OperateLinux.executeShell(command);
    }

    // sm2 签名
    /*
     * @description TODO
     * @Param sig_file: 加密生成的文件
     * @Param file_path: 源文件
     * @return com.zhb.Dto.Result
     * @author bigbo
     * @date 2023/5/8 22:44
     */
    public Result sm2Sign(String sig_file, String file_path) throws Exception {
        String command = "/root/tongsuo/bin/tongsuo dgst -sm3 -sign ./sm2.key -out "+sig_file+" "+file_path;
        return OperateLinux.executeShell(command);
    }
}
